package updown.config;

import com.github.pagehelper.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ExitCodeGenerator;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringApplicationRunListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;

/**
 * <p>Title: ww.kaixin.downupservice.config </p>
 * <p>Description:to-22-6-1: -  </p>
 *
 * @Author:jiangkaixin
 * @Date:2021/12/20
 * @Version: 1.0
 */
@Slf4j
@Component
public class ConfigCheckListener implements SpringApplicationRunListener {
    @Autowired
    private CommonConfig commonConfig;

    @Autowired
    private MyEurekaServerConfig myEurekaServerConfig;


    @Override
    public void starting() {
        System.out.println("正在启动 ===>>> ");
    }

    @Override
    public void started(ConfigurableApplicationContext context) {
        if (StringUtil.isEmpty(commonConfig.getStartScriptPrefix())
        ||  StringUtil.isEmpty(commonConfig.getScriptSuffix())
                ||  StringUtil.isEmpty(commonConfig.getKillScript())
                ||  StringUtil.isEmpty(commonConfig.getKillNineScript())
                ||  StringUtil.isEmpty(commonConfig.getScriptPath())
                ||  StringUtil.isEmpty(commonConfig.getJarPath())
                ||  StringUtil.isEmpty(myEurekaServerConfig.getServiceUrl())
        ){
            int exitCode = SpringApplication.exit(context, (ExitCodeGenerator) () -> 0);
            log.info("配置文件参数缺失，系统已关闭");
            System.exit(exitCode);
        }
    }
}
